package com.ruoyi.service.impl;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.domain.Ability_ServiceInfor;
import com.ruoyi.domain.Ability_UserLimitingInfor;
import com.ruoyi.mapper.Ability_ServiceInforMapper;
import com.ruoyi.mapper.Ability_UserLimitingInforMapper;
import com.ruoyi.service.Ability_UserLimitingService;
import com.ruoyi.service.ThrottlingKong;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service("Ability_UserLimitingServiceImpl")
public class Ability_UserLimitingServiceImpl implements Ability_UserLimitingService {
    @Autowired
    Ability_UserLimitingInforMapper ability_UserLimitingInforMapper;
    @Autowired
    Ability_ServiceInforMapper ability_ServiceInforMapper;
    @Autowired
    ThrottlingKong throttlingKong;


    @Override
    public List<Ability_UserLimitingInfor> selectAbility_UserLimitingInforList(Ability_UserLimitingInfor ability_UserLimitingInfor) {
        return ability_UserLimitingInforMapper.selectAbility_UserLimitingInforList(ability_UserLimitingInfor);
    }

    @Override
    public int deleteAbility_UserLimitingInforById(Integer id) {

        return ability_UserLimitingInforMapper.deleteAbility_UserLimitingInforById(id);
    }

    @Override
    public int insertAbility_UserLimitingInfor(Ability_UserLimitingInfor ability_UserLimitingInfor) {
        JSONObject requestBody = new JSONObject();
        JSONObject config=new JSONObject();
        String sername=ability_ServiceInforMapper.selectAbility_ServiceInforById(ability_UserLimitingInfor.getSid()).getServiceNameE();
        requestBody.put("name","rate-limiting");
        config.put("minute",ability_UserLimitingInfor.getLevel());
        config.put("policy","local");
        requestBody.put("config",config);
        JSONObject consumer=new JSONObject();
        consumer.put("id",ability_UserLimitingInfor.getPid());
        requestBody.put("consumer",consumer);
        JSONObject response = new JSONObject();
        try {
            Ability_ServiceInfor ability_ServiceInfor=ability_ServiceInforMapper.selectAbility_ServiceInforById(ability_UserLimitingInfor.getSid());
            response= throttlingKong.throttlingInService(requestBody,ability_ServiceInfor.getServiceNameE());
        }catch (Exception e){
            return -1;
        }

        if(response!=null&&response.getString("KongCode").equals("200")){
            String pid=response.getString("id");
            ability_UserLimitingInfor.setPid(pid);
         int k=ability_UserLimitingInforMapper.insertAbility_UserLimitingInfor(ability_UserLimitingInfor);
        if(k>0){
            return k;
        }else{
            return -2;
        }

        }else{
            return -1;
        }

    }

    @Override
    public Ability_UserLimitingInfor selectAbility_UserLimitingInforById(Integer id) {

        return ability_UserLimitingInforMapper.selectAbility_UserLimitingInforById(id);
    }

    @Override
    public int updateAbility_UserLimitingInfor(Ability_UserLimitingInfor ability_UserLimitingInfor) {
        Ability_UserLimitingInfor infor=ability_UserLimitingInforMapper.selectAbility_UserLimitingInforById(ability_UserLimitingInfor.getId());
        infor.setLevel(ability_UserLimitingInfor.getLevel());
       int k= ability_UserLimitingInforMapper.updateAbility_UserLimitingInfor(ability_UserLimitingInfor);
        JSONObject requestBody = new JSONObject();
        requestBody.put("name","rate-limiting");
        JSONObject config=new JSONObject();
        config.put("minute",infor.getLevel());
        requestBody.put("config",config);
        if(k>0){
            try {
                throttlingKong.updatethrottling(requestBody,infor.getPid());
            }catch (Exception e){
                e.printStackTrace();
                return -2;
            }

        }else {
            return -1;
        }
        return k;
    }

    @Override
    public List<Ability_UserLimitingInfor> selectAbility_UserLimitingInforByUid(Integer uid) {
        return null;
    }

    @Override
    public int deleteAbility_UserLimitingInforBySUID(Ability_UserLimitingInfor ability_UserLimitingInfor) {
          Ability_UserLimitingInfor infor=ability_UserLimitingInforMapper.selectAbility_UserLimitingInforBySUID(ability_UserLimitingInfor);
        System.out.println("-------"+infor.getPid());
          if(infor!=null) {
           try {

               throttlingKong.deleteThrottling(infor.getPid());
           }catch (Exception e){
               e.printStackTrace();
           }

       }else{ return -1;}
        return ability_UserLimitingInforMapper.deleteAbility_UserLimitingInforById(ability_UserLimitingInfor.getId());
    }
}
